package com.lyeducation.social;

import com.lyeducation.security.config.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
 * 社交登录配置主类 Created on 2018/1/8 0008.
 *
 * @author zlf
 * @email i@merryyou.cn
 * @since 1.0
 */
@Configuration
@EnableSocial
@Order(1)
public class SocialConfig extends SocialConfigurerAdapter {

    @Autowired(required = false)
    SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;
    @Autowired
    private DataSource dataSource;

    @Autowired
    private ConnectionSignUp myConnectionSignUp;

  /**
   * 社交登录配类
   *
   * @return
   */
  @Bean
  public SpringSocialConfigurer merryyouSocialSecurityConfig() {
    String filterProcessesUrl = SecurityConstants.DEFAULT_SOCIAL_PROCESS_URL;
    MerryyouSpringSocialConfigurer configurer =
        new MerryyouSpringSocialConfigurer(filterProcessesUrl);

      //2、threeProvider successHandler
      configurer.setSocialAuthenticationFilterPostProcessor(socialAuthenticationFilterPostProcessor);

    return configurer;
  }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
                connectionFactoryLocator, Encryptors.noOpText());
        if (myConnectionSignUp != null) {
            repository.setConnectionSignUp(myConnectionSignUp);
        }

        return repository;
    }
//  @Override
//  public UsersConnectionRepository getUsersConnectionRepository(
//      ConnectionFactoryLocator connectionFactoryLocator) {
//    JdbcUsersConnectionRepository repository =
//        new JdbcUsersConnectionRepository(
//            dataSource, connectionFactoryLocator, Encryptors.noOpText());
//    if (myConnectionSignUp != null) {
//      repository.setConnectionSignUp(myConnectionSignUp);
//    }
//    return repository;
//  }

  /**
   * 处理注册流程的工具类
   *
   * @param factoryLocator
   * @return
   */
  @Bean
  public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator factoryLocator) {
    return new ProviderSignInUtils(factoryLocator, getUsersConnectionRepository(factoryLocator));
  }
//未说明其作用
//  @Override
//  public UserIdSource getUserIdSource() {
//    return new AuthenticationNameUserIdSource();
//  }
}
